package com.wchen.gdoc.generate.mapping;

import com.wchen.gdoc.generate.mapping.data.ClassData;
import com.wchen.gdoc.generate.mapping.data.MethodData;
import com.wchen.gdoc.generate.resource.structure.ClassStructure;
import com.wchen.gdoc.generate.resource.structure.MethodStructure;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;


/**
 * 默认类结构解析器
 *
 * @author  wchen
 * @date    2019/11/7
 */
public class DefaultClassStructureResolver {
    private final static Logger LOGGER = LoggerFactory.getLogger(DefaultClassStructureResolver.class);

    private DefaultMethodStructureResolver methodStructureResolver = new DefaultMethodStructureResolver();

    /**
     * 解析类结构
     * @param classStructures
     * @return
     */
    public List<ClassData> resolveClassStructures(List<ClassStructure> classStructures) {

        List<ClassData> classDataList = new ArrayList<>();
        for(ClassStructure classStructure : classStructures){
            ClassData classData = new ClassData();
            classData.setClassCommentText(classStructure.getCommentText());
            List<MethodData> methodDataList = extractMethodData(classStructure);
            classData.setMethodDataList(methodDataList);
            classDataList.add(classData);
        }
        return classDataList;
    }

    /**
     * 解析structure
     * @param classStructure
     * @return
     */
    private List<MethodData> extractMethodData(ClassStructure classStructure){
        LOGGER.info(String.format("extractMethodDataList begin.  class:%s", classStructure.getClassName()));
        List<MethodData> methodDataList = new ArrayList<>();
        for (MethodStructure methodStructure : classStructure.getMethodStructureList()) {
            MethodData methodData = this.methodStructureResolver.resolve(classStructure, methodStructure);
            methodDataList.add(methodData);
        }
        return methodDataList;
    }
}
